﻿using EasyClassUI.forms.blog;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using EasyClassUI.WebRequest;
using EasyClassUI.entity;
using System.Threading.Tasks;

namespace EasyClassUI.forms.group
{
    public partial class UploadButton : Form
    {
        private StudyGroup group;
        public UploadButton(StudyGroup group)
        {
            InitializeComponent();
            this.group = group;
            groupNameLabel.Text = group.StudyGroupName;
            errMsg.Visible = false;
            initSourceList();
            initGroupMembers();
        }

        private void DisplayResources(StudyResource resource) {
            resourceCell rc = new resourceCell(resource.FileName, (resource.FileSize / (1024)).ToString().Substring(0, 4) + "KB", group.StudyGroupID);
            sourceListPanel.Controls.Add(rc);
        }
        private void DisplayMembers(string member) {
            groupMember gm = new groupMember(member);
            groupMemberPanel.Controls.Add(gm);
        }

        private void initSourceList()
        {
            // 显示所有群内文件
            if (group.StudyResources != null) {
                Parallel.ForEach(group.StudyResources, r => DisplayResources(r));
            }
        }

        private void initSourceListCustom(List<StudyResource> resources) {
            // 搜索群内文件
            if (group.StudyResources != null) {
                Parallel.ForEach(resources, r => DisplayResources(r));
            }
        }

        private void initGroupMembers()
        {
            // 显示所有组内成员
            GroupService groupService = new GroupService();
            QueryMemberStatus result = groupService.QueryAllMember(group.StudyGroupID.ToString());

            if (result.Status == "200") {
                Parallel.ForEach(result.Users, m => DisplayMembers(m));
            }

            else {
                MessageBox.Show("Please check your connection.");
            }
        }


        private void sourceListPanel_Paint(object sender, PaintEventArgs e)
        {

        }

        private void guna2Button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void groupBlogBtn_Click(object sender, EventArgs e)
        {
            allRequiredBlogsForm arbf = new allRequiredBlogsForm(group);
            arbf.Show();
        }

        private void searchButton_Click(object sender, EventArgs e) {
            // Search resources
            sourceListPanel.Controls.Clear();
            List<StudyResource> result = group.StudyResources.Where(p => p.FileName.Contains(SearchText.Text)).ToList();
            foreach(StudyResource resource in result) {
                resourceCell rc = new resourceCell(resource.FileName, (resource.FileSize / (1024)).ToString().Substring(0, 4) + "KB", group.StudyGroupID);
                sourceListPanel.Controls.Add(rc);
            }
        }

        private void UploadBtn_Click(object sender, EventArgs e) {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.Multiselect = true;//等于true表示可以选择多个文件
            dlg.Title = "Please select files to upload.";
            if (dlg.ShowDialog() == DialogResult.OK) {
                List<string> fileToUpload = dlg.FileNames.ToList();
                ResourceService resourceService = new ResourceService();
                string result = resourceService.UploadFileToGroup(fileToUpload, group.StudyGroupID.ToString());
                if (result == "200") {
                    errMsg.Visible = true;
                    errMsg.Text = "Upload succeeded!";
                }
                else {
                    errMsg.Visible = true;
                    errMsg.Text = "Something went wrong, please check your networks.";
                }
            }
        }
    }
}
